const Router = require('koa-router')
const { verifyToken } = require('@/utils/jwt')
const roleValidate = require('@/middlewares/validateMiddlewares/roleValidate')
const roleLog = require('@/middlewares/logMiddlewares/roleLog')
const roleCheckUpdate = require('@/middlewares/checkUpdateMiddlewares/roleUpdate')
const { verifyAuth } = require('@/middlewares/authMiddlewares/verifyAuth')
const {
  validateSlideCode
} = require('@/middlewares/validateMiddlewares/userValidate')
const roleController = require('@/controller/roleController')

const router = new Router()

/**
 * 获取角色列表
 */
router.get(
  '/list',
  verifyToken(),
  roleValidate.listRoleValidate,
  roleController.listRole
)

/**
 * 获取小于当前用户的角色列表
 */
router.get(
  '/auth/list',
  verifyToken(),
  roleValidate.listRoleValidate,
  roleController.listAuthRole
)

/**
 * 创建角色列表
 */
router.post(
  '/create',
  verifyToken(),
  verifyAuth(['admin.role.edit', 'admin.sourceAuth.edit']),
  roleValidate.createRoleValidate,
  roleLog.createRoleLog,
  roleController.createRole
)

/**
 * 更新角色列表
 */
router.put(
  '/update',
  verifyToken(),
  verifyAuth(['admin.role.edit', 'admin.sourceAuth.edit']),
  roleValidate.updateRoleValidate,
  roleCheckUpdate.singerUpdateCheck,
  roleLog.updateRoleLog,
  roleController.updateRole
)

/**
 * 删除角色列表
 */
router.delete(
  '/delete',
  validateSlideCode(),
  verifyToken(),
  verifyAuth(['admin.role.edit', 'admin.sourceAuth.edit']),
  roleValidate.delRolesValidate,
  roleCheckUpdate.delRolesSave,
  roleLog.delRoleLog,
  roleController.delRoles
)

/**
 * 角色排序
 */
router.put(
  '/sort',
  verifyToken(),
  verifyAuth(['admin.role.edit', 'admin.sourceAuth.edit']),
  roleValidate.sortRolesValidate,
  roleController.sortRoles
)

module.exports = router
